import logging

import pytest

from resource.pom.hap import Hap
from resource.pom.settings import Settings


@pytest.fixture(autouse=True)
def uninstall_hap(device):
    logging.info('预置条件：镜像版本正确、联网正常，浏览器hap安装正常')
    device.install_hap(Hap.package_path)
    connect_status = Settings.connect_wifi(device)
    assert connect_status, 'wifi连接失败'
    yield
    try:
        device.uninstall_hap(Hap.bundle_name)
    except:
        pass


class Test:
    app = Hap

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.打开浏览器')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.wait(5)
        logging.info('步骤2.浏览器中打开百度新闻，打开正常，有结果1')
        device.refresh_layout()
        input_url = device.get_element_by_key('inputUrl')
        x, y = device.center_of_element(input_url)
        device.input_text(x, y, 'https://news.baidu.com/')
        if device.soft_keyboard_area():
            device.close_soft_keyboard()
        device.click_element(device.get_element_by_key('actionButton0'))
        device.wait(2)
        device.click_element(device.get_element_by_key('actionButton0'))
        logging.info('预期结果1.新闻打开正常')
        device.wait(8)
        device.refresh_layout()
        device.assert_text_exist('网页')
        device.assert_text_exist('新闻')
        device.assert_text_exist('贴吧')
        device.assert_text_exist('百度新闻')
        device.assert_text_exist('百度一下')
        device.assert_text_exist('登录')
        device.assert_text_exist('注册')
        device.assert_type_exist('TitleBar')
        device.assert_type_exist('Web')
        device.assert_type_exist('genericContainer')
        device.assert_type_exist('ToolBar')
